package upicgame.cart.controller;

import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import upicgame.cart.service.CartService;
import upicgame.entity.CartItem;

@Controller
public class CartController {
	@Autowired
	private CartService cs;

	@RequestMapping("items")
	public String getCartItem(@RequestParam(value = "uid", required = true) Integer uid, Model model) {
		try {
			if (uid != null) {
				Map<String, CartItem> map = cs.getCartItem(uid);

				double totalPrice = cs.getTotalPrice(map);
				model.addAttribute("totalPrice", totalPrice);
				model.addAttribute("cart", map);
				return "/showcart.jsp";
			} else {
				model.addAttribute("msg", "未登录");
				return "redirect:/login_register.jsp";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/index.jsp";
		}
	}

	@RequestMapping("addCartItem")
	public String addCartItem(@RequestParam(value = "uid", required = true) Integer uid,
			@RequestParam(value = "gid", required = true) Integer gid, Model model) {
		if (uid != null) {
			boolean result = cs.addCartItem(uid, gid);
			if (result) {
				model.addAttribute("msg", "SUCCESS");
				return "redirect:/items?uid=" + uid;
			} else {
				model.addAttribute("msg", "FAILED");
				return "redirect:/user_gameList?pageNum=1";
			}
		} else {
			model.addAttribute("msg", "未登录");
			return "redirect:/login_register.jsp";
		}
	}

	@RequestMapping("delCartItem")
	public String delCartItem(@RequestParam(value = "uid", required = true) Integer uid,
			@RequestParam(value = "gid", required = true) Integer gid, Model model) {
		boolean result = cs.delCartItem(uid, gid);
		if (result) {
			model.addAttribute("msg", "SUCCESS");
			return "redirect:/items?uid=" + uid;
		} else {
			model.addAttribute("msg", "FAILED");
			return "redirect:/items?uid=" + uid;
		}
	}

	@RequestMapping("clearCartItem")
	public String delCartItem(@RequestParam(value = "uid", required = true) Integer uid, Model model) {
		boolean result = cs.clearCartItem(uid);
		if (result) {
			model.addAttribute("msg", "SUCCESS");
			return "redirect:/items?uid=" + uid;
		} else {
			model.addAttribute("msg", "FAILED");
			return "redirect:/items?uid=" + uid;
		}
	}

//	@RequestMapping("items/{uid}")
//	public String getCartItemList(@PathVariable("uid") Integer uid, HttpServletRequest request,
//			HttpServletResponse response) {
//		List<CartItem> list = cs.getCartItemInCookie(uid, request);
//		if (list != null) {
//			double totalPrice = cs.getTotalPrice(list);
//			request.setAttribute("totalPrice", totalPrice);
//			request.setAttribute("cart", list);
//			return "/showcart.jsp";
//		} else {
//			List<CartItem> items = cs.getCartItemList(uid);
//			try {
//				if (items != null) {
//					double totalPrice = cs.getTotalPrice(items);
//					Cookie cookie = null;
//					cookie = new Cookie(uid.toString(), URLEncoder.encode(cs.toJsonCart(items), "UTF-8"));
//					cookie.setMaxAge(60);
//					cookie.setPath("/");
//					response.addCookie(cookie);
//					request.setAttribute("totalPrice", totalPrice);
//					request.setAttribute("cart", items);
//					return "/showcart.jsp";
//				}
//			} catch (UnsupportedEncodingException e) {
//				e.printStackTrace();	
//			}
//			return "/showcart.jsp";
//		}
//	}
//
//	@RequestMapping("addCartItem/{uid}/{gid}")
//	public String addCartItem(@PathVariable("uid") Integer uid, @PathVariable("gid") Integer gid,
//			HttpServletRequest request, HttpServletResponse response) {
//		String result = cs.addCartItem(uid, gid, request);
//		Cookie cookie = null;
//		try {
//			if (!result.equals("false")) {
//				cookie = new Cookie(uid.toString(), URLEncoder.encode(result, "UTF-8"));
//				cookie.setMaxAge(60);
//				cookie.setPath("/");
//				response.addCookie(cookie);
//				request.setAttribute("msg", "添加成功");
//				return "redirect:items/"+uid;
//			} else {
//				request.setAttribute("msg", "添加失败");
//				return "";
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//			return "";
//		}
//	}

}
